import datetime as dt

import numpy as np

from matplotlib.dates import date2num

def mkbool(floating):
    if str(floating) == '1':
        return True
    elif float(floating) > 0:
        print floating
        return False
    else:
        return False

def mkdate2(text):
    return date2num(dt.datetime.strptime(text, '%y-%m-%d %H:%M'))

def mkdatess2(text):
    return date2num(dt.datetime.strptime(text, '%y-%m-%d %H:%M:%S'))

def mkdatess(text):
    return dt.datetime.strptime(text, '%y-%m-%d %H:%M:%S')

def mkdateyear(text):
    return dt.datetime.strptime(text, '%Y-%m-%d')

def date_idx2(data, start_date, stop_date):
    return np.where([time < stop_date and
                     (time > start_date)
                     for time in data[0][:]['Time']])

def date_idx(data, start_date, stop_date):
    return np.where([time <= stop_date and (time > start_date) for time in data['Time']])

def get_data_at_date(data, start_date, stop_date):
    idx = date_idx(data, start_date, stop_date)
    new_data = data.copy()
    for key, value in data.iteritems():
        new_data[key] = value[idx]
    return new_data

def get_specific_data(data, parameters):
    """Simple function to construct array of selected parameters
    """
    for i, para in enumerate(parameters):
        if i == 0:
            ret_data = np.array([data[para]]).T
        else:
            ret_data = np.hstack((ret_data, np.array([data[para]]).T))
    return ret_data
